package com.project.base;

import com.project.biz.RedisBiz;
import com.project.common.enums.LoginEnum;
import com.project.common.model.UserInfo;
import com.project.common.utils.ThreadLocalBuilder;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;

/**
* @Description:   公共Controller
* @Author:         wangzhiqiang
* @CreateDate:     2019-07-18 11:04
* @Version:        1.0
*/
@Slf4j
@Component
public class BaseController {

    /**
     * 用户缓存信息
     */
    @Autowired
    private RedisBiz redisBiz;

    /**
     * 获取当前登录TOKEN
     * @return
     */
    public String getToken(){
        return ThreadLocalBuilder.getToken();
    }

    /**
     * 获取当前请求
     * @return
     */
    public HttpServletRequest getHttpServletRequest(){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes())
                .getRequest();
        return request;
    }

    /**
     * 获取当前登录用户
     * @return
     */
    public UserInfo getLoginUser(){
//        UserInfo userInfo = new UserInfo();
//        userInfo.setBaseUserId(1273214180564742146l);
//        return userInfo;
        Long userId = this.redisBiz.getUserInfo(this.getToken());
        UserInfo userInfo = new UserInfo();
        userInfo.setBaseUserId(userId);

        return userInfo;
    }

    /**
     * 根据名字获取cookie
     * @param request
     * @param name cookie名字
     * @return
     */
    public static Cookie getCookieByName(HttpServletRequest request,String name){
        Map<String,Cookie> cookieMap = readCookieMap(request);
        if(cookieMap.containsKey(name)){
            Cookie cookie = cookieMap.get(name);
            return cookie;
        }else{
            return null;
        }
    }

    /**
     * 将cookie封装到Map里面
     * @param request
     * @return
     */
    private static Map<String, Cookie> readCookieMap(HttpServletRequest request){
        Map<String,Cookie> cookieMap = new HashMap<>();
        Cookie[] cookies = request.getCookies();
        if(null!=cookies){
            for(Cookie cookie : cookies){
                cookieMap.put(cookie.getName(), cookie);
            }
        }
        return cookieMap;
    }

    /**
     * 获取用户真实IP
     * @return
     */
    public String getRealIP(){
        HttpServletRequest request = this.getHttpServletRequest();

        return this.getRealIP(request);
    }

    /**
     * 获取用户真实IP
     * @return
     */
    public static String getRealIP(HttpServletRequest request){
        String ip = request.getHeader("X-Real-IP");
        if(!StringUtils.isBlank(ip) && !"unknown".equalsIgnoreCase(ip))
            return ip;
        ip = request.getHeader("x-forwarded-for");

        if(ip==null||ip.length()==0||"unknown".equalsIgnoreCase(ip))
            ip = request.getHeader("Proxy-Client-IP");

        if(ip==null||ip.length()==0||"unknown".equalsIgnoreCase(ip))
            ip = request.getHeader("WL-Proxy-Client-IP");

        if(ip==null||ip.length()==0||"unknown".equalsIgnoreCase(ip)){
            ip = request.getRemoteAddr();
            if("0:0:0:0:0:0:0:1".equals(ip))
                try {
                    ip= InetAddress.getLocalHost().getHostAddress();
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                }
        }
        return ip;
    }

}
